package com.naiterui.ehp.bs.wechat.service.impl;

import org.springframework.stereotype.Service;

import com.naiterui.ehp.bs.wechat.config.WeChatConfig;
import com.naiterui.ehp.bs.wechat.properties.WeChatProperties;
import com.naiterui.ehp.bs.wechat.protocol.result.AuthAccessToken;
import com.naiterui.ehp.bs.wechat.service.IWebAuthorizeApiService;
import com.naiterui.ehp.bs.wechat.utils.WeChatApiUtil;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhoupeipei
 * @date 2019-06-24 16:41
 * @since 1.0.0
 */
@Service
@Slf4j
@AllArgsConstructor
public class WebAuthorizeApiServiceImpl implements IWebAuthorizeApiService {

    private final WeChatConfig weChatConfig;

    @Override
    public String getWebAuthorizeUrlSilent(String redirectUrl, String params) {
        WeChatProperties properties = this.weChatConfig.paProperties();
        return WeChatApiUtil.getWebAuthorize(properties.getAppId(), redirectUrl, true, params);
    }

    @Override
    public String getWebAuthorizeUrlSilent(String redirectUrl, String params, String hrefText) {
        String url = this.getWebAuthorizeUrlSilent(redirectUrl, params);
        return "<a href=\"" + url + "\">" + hrefText + "</a>";
    }

    @Override
    public String getWebAuthorizeUrl(String redirectUrl, String params) {
        WeChatProperties properties = this.weChatConfig.paProperties();
        return WeChatApiUtil.getWebAuthorize(properties.getAppId(), redirectUrl, false, params);
    }

    @Override
    public String codeForOpenId(String code) {
        WeChatProperties properties = this.weChatConfig.paProperties();
        AuthAccessToken authAccessToken = WeChatApiUtil.getWebAuthorizeAccesstoken(properties.getAppId(), properties.getAppSecret(), code);
        return authAccessToken.getOpenId();
    }

}
